< previous page page_38 next page >

Page 38
TextWidth and TextHeight Methods
While not exactly text properties, the TextWidth and TextHeight methods are extremely useful for managing text placement. The form, picture box, and printer objects all support the TextWidth and TextHeight functions.
TextWidth takes as a parameter a string that you might want to display. It returns the width of the string in the drawing object's current scale mode. For example, TextWidth for a form using a ScaleMode of vbTwips returns the width of its input string in twips as it would appear displayed on the form using the form's current font. Similarly, the TextHeight method returns the height of a string.
These methods are extremely useful for positioning text because many fonts have characters of variable width. That means you cannot calculate the width of a string by multiplying a simple character width by the number of characters in the string. For instance, in 12-point Arial, the width of the string ''WIDTH" is 750 twips, while the width of the string "width" is only 540 twips. The characters in the first string are wider than those in the second.
Although not all types of controls have TextWidth and TextHeight methods, you can use the methods provided by a control's parent form to perform necessary calculations. For example, you might want to know how large a button's caption is, but the command button control does not provide TextWidth and TextHeight functions. You can use the button's parent form to calculate the caption's size.
First set the parent form's Font property so that it matches the Font property of the control. Then use the form's TextWidth and TextHeight functions to find the string's size. The TextWidthAndHeight subroutine shown in the following code uses this method to compute the height and width of a string.
e10a6a0a59f035e870d90cca05b85fce.gif
Private Sub TextWidthAndHeight(ByVal ctl As Control, _

    ByVal txt As String, ByRef wid As Single, ByRef hgt As Single)
Dim frm As Form

    Set frm = ctl.Parent
    Set frm.Font = ctl.Font
    wid = frm.TextWidth(txt)
    hgt = frm.TextHeight(txt)
End Sub
Other Graphic Properties
Controls have many other properties that modify their graphic appearance. Some of the more important of these are described in the following sections.

 
< previous page page_38 next page >